package com.itheima.reggie.controller;

import com.itheima.reggie.domain.Category;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//分类模块
@RestController     //Response + Controller
@Api(tags = "分类相关接口")
public class CategoryController {

    //依赖注入
    @Autowired
    private CategoryService categoryService;

    //查询分类列表
    @GetMapping("/category/findAll")
    @ApiOperation("查询分类列表")
    public ResultInfo findAll(){
        //1. 调用service层
        List<Category> list = categoryService.findAll();

        //2. 返回响应
        return ResultInfo.success(list);

    }

    //添加分类
    @PostMapping("/category")
    @ApiOperation("添加分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类对象", required = true),
    })
    public ResultInfo save(@RequestBody Category category){     //接收请求体中的参数
        //1. 调用service层
        categoryService.save(category);
        //2. 返回响应
        return ResultInfo.success();
    }

    // 修改分类
    @PutMapping("/category")
    @ApiOperation("修改分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类对象", required = true),
    })
    public ResultInfo updateById(@RequestBody Category category){   //接收请求体中的参数
        //1.调用service层
        categoryService.updateById(category);

        //2.返回响应
        return ResultInfo.success();
    }

    //删除分类
    @DeleteMapping("/category")
    @ApiOperation("删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类id", required = true),
    })
    public ResultInfo deleteById(Long id){  //接收地址中的被?分隔的参数,spring内置自动获取
        //1. 调用service层
        categoryService.deleteById(id);     //使用全局异常类处理,不需要返回值
        //2. 返回响应
        return ResultInfo.success();
    }

}
